package rxt.huiyi.com.rxterfit.retrofitService;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rxt.huiyi.com.rxterfit.BuildConfig;
import rxt.huiyi.com.rxterfit.constant.Constants;

/**
 * Created by huiyi on 2017/4/20.
 */
@Module
public class ReterfitService {

    public OkHttpClient okHttpClient;
    public Retrofit retrofit;

    @Provides
    @Singleton
    public Retrofit getReterfitService() {

        if(BuildConfig.DEBUG) {

            okHttpClient = new OkHttpClient();
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);

            okHttpClient = new OkHttpClient.Builder().
                    addInterceptor(logging)
                    //.connectTimeout(30, TimeUnit.SECONDS)
                    .build();

            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(Constants.baseURL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
            return retrofit;
        } else {
            okHttpClient = new OkHttpClient();
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.NONE);

            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(Constants.baseURL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
            return retrofit;
        }

    }

//    public <T> T createApi(Class<T> cls) {
//        return retrofit.create(cls);
//    }

}
